package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.*;
import com.cqjtu.drive.pojo.*;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.CoachService;
import com.cqjtu.drive.service.GradeService;
import com.cqjtu.drive.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zxl
 * @Date 2024/6/6 12:39
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    CoachMapper coachMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    AdministrationMapper administrationMapper;

    @Autowired
    GradeService gradeService;
    @Autowired
    GradeMapper grademapper;
    @Override
    public Response<User> register(Map<String, String> map) {
        List<User> list = userMapper.getAllUsers();
        String userId;
        if(list.isEmpty()){
            userId = "1";
        } else {
            int a = Integer.parseInt(list.get(list.size() - 1).getUserId()) + 1;
            userId = Integer.toString(a);
        }
        String userAccount = map.get("userAccount");
        String userPassword = map.get("userPassword");
        int userType=Integer.parseInt(map.get("userType"));
        try{
            userMapper.insertUser(new User(
                    userId,
                    userAccount,
                    userPassword,
                    userType
            ));
            if(userType == 1){//教练
                Coach coach = new Coach();
                coach.setUserId(userId);
                List<Coach> list1 = coachMapper.getAllCoaches();
                if(list1 != null && !list1.isEmpty()){
                    int b = Integer.parseInt(list1.get(list1.size() - 1).getcId()) + 1;
                    String cId = Integer.toString(b);
                    coach.setcId(cId);
                }else {
                    coach.setcId("1");
                }
                coachMapper.insertCoach(coach);
            }else if(userType == 2){//学员
                Student student = new Student();
                student.setUserId(userId);
                List<Student> students = studentMapper.getAllStudents();
                String sId;
                if(students != null && !students.isEmpty()){
                    int c = Integer.parseInt(students.get(students.size() - 1).getsId()) + 1;
                     sId = Integer.toString(c);
                    student.setsId(sId);
                }else {
                    student.setsId("1");
                    sId="101";
                }
                student.setsAccept(0);
                student.setsDay(0);
                student.setsTestnum(0);
                studentMapper.insertStudent(student);
                Grade g=new Grade();
                g.setsId(sId);
                gradeService.insertGrade(g);
                File folder = new File("D:\\driveinfo\\"+sId);
                folder.mkdir();
            }else if(userType == 3) {//管理员
                Administration administration = new Administration();
                administration.setUserId(userId);
                List<Administration> administrations = administrationMapper.getAllAdministrations();
                if(administrations != null && !administrations.isEmpty()){
                    int c = Integer.parseInt(administrations.get(administrations.size() - 1).getaId()) + 1;
                    String aId = Integer.toString(c);
                    administration.setaId(aId);
                }else {
                    administration.setaId("1");
                }
                administrationMapper.insertAdministration(administration);
            }
            return new Response<>(true,"Success");
        }catch (Exception e){
            return new Response<>(false,"Error");
        }
    }

    @Override
    public Response<User> login(String userName,String passWord) {

        User user = userMapper.getUserByAccount(userName);
        if(user==null){
            return new Response<>(false,"用户不存在");
        }else if(!user.getUserPassword().equals(passWord)){
            return new Response<>(false,"密码错误");
        }else {
            String userType = Integer.toString(user.getUserType());
            return new Response<>(true,userType);
        }
    }

    @Override
    public Response<User> delete(String userId){
        try{
            userMapper.deleteUser(userId);
            return new Response<>(true,"Success");
        }catch (Exception e){
            return new Response<>(false,"Error");
        }
    }

    @Override
    public Response<List<User>> getAll() {
        List<User> users = userMapper.getAllUsers();
        if (users != null && !users.isEmpty()) {
            return new Response<>(true, "Success", users);
        } else {
            return new Response<>(false, "No users found");
        }
    }

    @Override
    public Response<User> getByAccount(String userAccount) {
        System.out.println(userAccount);
        User user = userMapper.getUserByAccount(userAccount);

        if (user != null) {
            return new Response<>(true, "User found", user);
        } else {
            return new Response<>(false, "User not found");
        }
    }
}
